#include <string>
#include <iostream>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"

#include "Common.h"

using analyzer::HeaderFileAnalyzer;
using std::cout;
using std::endl;
using namespace boost::program_options;

/**
 * This is the main entry point of the application.
 */
int main(int argc, char* argv[]) {

	bool recursive = false;
	std::string path;

	options_description desc("Allowed options");
	desc.add_options()
		("help,h", "Prints help message")
		("recursive,r", "Searches directories recursively")
		("version,v", "Prints out version information")
		("debug,d", "Makes more verbose output")
		("path", "The directory to analyze");

	positional_options_description p;
	p.add("path", 1);

	variables_map vm;
	store(command_line_parser(argc, argv)
		.options(desc)
		.positional(p)
		.run(), vm);
	notify(vm);    
	
	if(vm.count("recursive")) {
		recursive = true;
	}
	
	if(vm.count("version")) {
		cout << "CycleFinder Version 1.0" << endl;
		return 1;
	}

	if(vm.count("debug")) {
		analyzer::setLogLevel(analyzer::DEBUG);
	}

	if (vm.count("help") || !vm.count("path")) {
		cout << desc << endl;
		return 1;
	}	

	HeaderFileAnalyzer analyzer;
	analyzer.setIncludeSubDirectories(recursive);
	
	try {
		analyzer.scanDirectory(vm["path"].as<std::string>());
	}
	catch (boost::filesystem3::filesystem_error& e) {
	//	std::cout << e.system_error::what() << std::endl;
		std::cerr << e.what() << std::endl;
		return 1;
	}
	return 0;
}
